package by.demchenko.sample.services.impl;

import by.demchenko.sample.dataaccess.base.SectionDao;
import by.demchenko.sample.datamodel.Section;
import by.demchenko.sample.services.SectionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.inject.Inject;
import java.util.List;

@Service
public class SectionServiceImpl implements SectionService {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(SectionServiceImpl.class);

    @Inject
    private SectionDao dao;

    @Override
    public Section get(Integer id) {
        return dao.getById(id);
    }

    @Override
    public void save(Section section) {
        dao.insert(section);
    }

    @Override
    public void saveOrUpdate(Section section) {
        if (section.getId() == null) {
            LOGGER.debug("Save new: {}", section);
            dao.insert(section);
        } else {
            LOGGER.debug("Update: {}", section);
            dao.update(section);
        }
    }

    @Override
    public void delete(Integer id) {
        dao.delete(id);
    }

    @Override
    public List<Section> getAll() {
        return dao.findAll();
    }
}
